<?php

namespace App\Services\Scenic\Order;

use App\Services\Scenic\Tuniu\TuniuService;

class TuniuOrderFactory implements ScenicOrderFactoryInterface
{

    public function bankOrder(array $order_data): string
    {
        $sourceOrderId = $order_data['out_order_sn'];
        $productId = $order_data['order_params']['ticket_id'];
        $startTime = $order_data['order_params']['play_at'];
        $bookNumber = $order_data['order_params']['count'];
        $contact = [
            'contactName'=>$order_data['contact']['truename'],
            'contactTel'=>$order_data['contact']['mobile'],
        ];
        if (!empty($order_data['contact']['identity_type'])){
            $contact['psptType']=$order_data['contact']['identity_type'];
        }
        if (!empty($order_data['contact']['identity_code'])){
            $contact['psptId']=$order_data['contact']['identity_code'];
        }

        if (!empty($order_data['contact']['email'])){
            $contact['contactEmail'] = $order_data['contact']['email'];
        }

        $touristList = [];
        $passengers = $order_data['passengers'] ?? [];
        foreach ($passengers as $passenger){
            $touristList[] = [
                'name'=>$passenger['truename'],
                'psptType'=>$passenger['identity_type'],
                'psptId'=>$passenger['identity_code'],
                'tel'=>$passenger['mobile'],
                'email'=>$passenger['email'] ?? '',
            ];
        }
        //获取对应的三方服务
        $scenicService = new TuniuService();

        $res = $scenicService->createOrder($sourceOrderId, $productId, $startTime, $bookNumber, $contact, $touristList);
//        if ($res->isNewFlag == 0){
//
//        }
        return $res->orderId;
    }


    public function orderStatus(array $order_data) :int
    {
        $orderId = $order_data['out_order_id'] ?? '';
        $scenicService = new TuniuService();

        return $scenicService->orderStatus($orderId);
    }


    public function submitOrder(array $order_data) :array
    {
        $orderId = $order_data['out_order_id'] ?? '';
        $payAmount = $order_data['price'] ?? 0;
        //获取对应的三方服务
        $scenicService = new TuniuService();

        $res = $scenicService->submitOrder($orderId,$payAmount);
        return [
            'order_id'=>$res->orderId,
            'out_trade_no'=>$res->outTradeNo,
        ];
    }

    public function orderDetail(array $order_data) :array
    {
        $orderId = $order_data['out_order_id'] ?? '';
        $scenicService = new TuniuService();
        $res = $scenicService->orderDetail($orderId);
        $orderInfo = $res->orderInfo;
        $orderStatus = $res->orderStatus;
        $verification_certificate = [];
        if (!empty($orderInfo->enterCertificate)){
            $attachments = [];
            if (!empty($orderInfo->enterCertificate->enterCertificateTypeInfo)){
                foreach ($orderInfo->enterCertificate->enterCertificateTypeInfo as $item){
                    if (!empty($item->ticketCertInfos)){
                        foreach ($item->ticketCertInfos as $certInfo){
                            $attachments[] = [
                                'type' => $certInfo->certType,
                                'value' => $certInfo->fileUrls,
                            ];
                        }
                    }

                }
            }
            $verification_certificate = [
                'txt'=>$orderInfo->enterCertificate->enterCertificateTxt,
                'attachment'=>$attachments,
            ];
        }
        $contact = [
            'name'=>$orderInfo->contactName,
            'mobile'=>$orderInfo->contactTel,
            'email'=>$orderInfo->contactEmail,
            'identity_type'=>$orderInfo->psptType,
            'identity_code'=>$orderInfo->psptId,
        ];
        return [
            'provider_ticket_id'=>$orderInfo->productId,
            'scenic_name'=>$orderInfo->scenicName,
            'scenic_id'=>$orderInfo->scenicId,
            'ticket_name'=>$orderInfo->productName,
            'play_at'=>$orderInfo->planDate,
            'verification_certificate'=>$verification_certificate,
            'ticket_office'=>$orderInfo->pickUpAddress,
            'book_number'=>$orderInfo->bookNumber,
            'contact'=> $contact,
        ];
    }

    public function orderRefund(array $order_data) :array
    {
        $orderId = $order_data['out_order_id'] ?? '';
        $causeType = $order_data['cause_type'] ?? '';
        $causeContent = $order_data['cause_content'] ?? '';
        //获取对应的三方服务
        $scenicService = new TuniuService();

        return  $scenicService->refundTicket($orderId,$causeType,$causeContent);
    }


    public function cancelOrder(array $order_data) :array
    {
        $orderId = $order_data['out_order_id'] ?? '';
        $remark = $order_data['remark'] ?? '';
        //获取对应的三方服务
        $scenicService = new TuniuService();
        try{
            $res = $scenicService->cancelOrder($orderId,$remark);
        }catch (\Exception $exception){
            return [
                'success'=>false,
                'msg'=>$exception->getMessage(),
            ];
        }
        return [
            'success'=>$res,
            'msg'=>'',
        ];
    }
}